import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'

const routes = [
  {
    path: '/',
    name: '登录',
    component: () => import('@/views/login'),
  },
  {
    path: '/registered',
    name: '注册',
    component: () => import('@/views/registered'),
  },
  {
    path: '/forgetPass',
    name: '忘记密码',
    component: () => import('@/views/forgetPass'),
  },
  {
    path: '/changePass',
    name: '修改手机号',
    component: () => import('@/views/changePass'),
  },
  {
    path: '/editPass',
    name: '修改密码',
    component: () => import('@/views/editPass'),
  },
  {
    path: '/agreement',
    name: '用户协议',
    component: () => import('@/views/agreement'),
  },
  {
    path: '/home',
    name: '主页',
    component: () => import('@/views/home'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: '欢迎',
        component: () => import('@/views/welcome'),
      },
      {
        path: '/myStore',
        name: '平台管理员',
        redirect: '/employeesMng',
        children: [
          {
            path: '/employeesMng',
            name: '管理员',
            component: () => import('@/views/employeesMng'),
            children: [],
          },
          {
            path: '/jobsMng',
            name: '岗位管理',
            component: () => import('@/views/jobsMng'),
            children: [],
          },
        ],
      },
      {
        path: '/storeInfo',
        name: '商户管理',
        component: () => import('@/views/storeInfo'),
        children: [
          {
            path: '/storeInfo/editor',
            name: '开户',
            component: () => import('@/views/storeInfo/editor'),
          },
          {
            path: '/storeInfo/edit',
            name: '编辑',
            component: () => import('@/views/storeInfo/edit'),
          },
          {
            path: '/storeInfo/details',
            name: '商户详情',
            component: () => import('@/views/storeInfo/details'),
          },
        ],
      },
      {
        path: '/goodsMng',
        name: '商品管理',
        component: () => import('@/views/goodsMng'),
        redirect: '/goodsTypeMng',
        children: [
          {
            path: '/goodsTypeMng',
            name: '商品属性管理',
            component: () => import('@/views/goodsMng/goodsTypeMng'),
          },
          {
            path: '/goods',
            name: '商品',
            component: () => import('@/views/goodsMng/goods'),
            redirect: '/goods/list',
            children: [
              {
                path: '/goods/list',
                name: '商品列表',
                component: () => import('@/views/goodsMng/goods/list'),
              },
              {
                path: '/goods/add',
                name: '新增商品',
                component: () => import('@/views/goodsMng/goods/add'),
              },
              {
                path: '/goods/edit',
                name: '商品编辑',
                component: () => import('@/views/goodsMng/goods/edit'),
              },
              {
                path: '/goods/details',
                name: '商品详情',
                component: () => import('@/views/goodsMng/goods/details'),
              },
            ],
          },
        ],
      },
      {
        path: '/prodApi',
        name: '生产接口管理',
        component: () => import('@/views/prodApi'),
        redirect: '/prodApi/list',
        children: [
          {
            path: '/prodApi/list',
            name: '生产接口',
            component: () => import('@/views/prodApi/list'),
          },
          {
            path: '/prodApi/goods',
            name: '平台商品',
            component: () => import('@/views/prodApi/goods'),
          },
          {
            path: '/prodApi/rechargeFlow',
            name: '上游充值记录',
            component: () => import('@/views/prodApi/rechargeFlow'),
          },
          {
            path: '/prodApi/yueDetail',
            name: '余额明细',
            component: () => import('@/views/prodApi/yueDetail'),
          },
        ]
      },
      {
        path: '/distApi',
        name: '下游接口管理',
        component: () => import('@/views/distApi'),
      },
      {
        path: '/orderMng',
        name: '订单管理',
        component: () => import('@/views/orderMng'),
        redirect: '/dlOrder',
        children: [
          {
            path: '/dlOrder',
            name: '代理订单',
            component: () => import('@/views/orderMng/dlOrder'),
            children: [
              {
                path: '/dlOrder/detail',
                name: '代理订单详情',
                component: () => import('@/views/orderMng/detail'),
              }
            ],
          },
          {
            path: '/fxOrder',
            name: '分销订单',
            component: () => import('@/views/orderMng/fxOrder'),
            children: [
              {
                path: '/fxOrder/detail',
                name: '分销订单详情',
                component: () => import('@/views/orderMng/detail'),
              }
            ],
          },
        ],
      },
      {
        path: '/accountMng',
        name: '商户资产管理',
        component: () => import('@/views/accountMng'),
        redirect: '/accountMng/list',
        children: [
          {
            path: '/accountMng/list',
            name: '商户资产',
            component: () => import('@/views/accountMng/list'),
          },
          {
            path: '/accountMng/flow',
            name: '线下代充记录',
            component: () => import('@/views/accountMng/flow'),
          },
          {
            path: '/accountMng/chongzhi',
            name: '商户充值记录',
            component: () => import('@/views/accountMng/chongzhi'),
          },
          {
            path: '/accountMng/xiaofei',
            name: '商户消费明细',
            component: () => import('@/views/accountMng/xiaofei'),
          },
          {
            path: '/accountMng/fenrun',
            name: '商户分润明细',
            component: () => import('@/views/accountMng/fenrun'),
          },
          {
            path: '/accountMng/tixian',
            name: '商户提现明细',
            component: () => import('@/views/accountMng/tixian'),
          },
          {
            path: '/accountMng/dongjie',
            name: '冻结佣金明细',
            component: () => import('@/views/accountMng/dongjie'),
          },
        ],
      },
      {
        path: '/inventory',
        name: '商品库存管理',
        redirect: '/inventory/list',
        children: [
          {
            path: '/inventory/list',
            name: '商品库存',
            component: () => import('@/views/inventory/list'),
          },
          {
            path: '/inventory/detail',
            name: '库存详情',
            component: () => import('@/views/inventory/detail'),
          },
        ],
      },
      {
        path: '/msgMng',
        name: '系统消息',
        component: () => import('@/views/msgMng'),
        children: [],
      },
      {
        path: '/sysMng',
        name: '系统管理',
        redirect: '/collectionAccount',
        children: [
          {
            path: '/collectionAccount',
            name: '收款账户管理',
            component: () => import('@/views/sysMng/collectionAccount'),
          },
          {
            path: '/presenterMng',
            name: '推荐人管理',
            component: () => import('@/views/sysMng/presenterMng'),
          },
          {
            path: '/shopControlMng',
            name: '商户活动价配置',
            component: () => import('@/views/sysMng/shopControlMng'),
          },
          {
            path: '/shopPriceMng',
            name: '特殊价格配置',
            component: () => import('@/views/sysMng/shopPriceMng'),
          },
          {
            path: '/shopChannelMng',
            name: '商户渠道配置',
            component: () => import('@/views/sysMng/shopChannelMng'),
          },
          {
            path: '/goodsControl',
            name: '商品购买配置',
            component: () => import('@/views/sysMng/goodsControl'),
          },
        ],
      },
      {
        path: '/materialMng',
        name: '素材管理',
        component: () => import('@/views/materialMng'),
        children: [],
      },
      {
        path: '/statistics',
        name: '数据统计',
        redirect: '/financialStatement',
        children: [
          {
            path: '/financialStatement',
            name: '财务报表',
            component: () => import('@/views/statistics/financialStatement'),
          },
          {
            path: '/dailyNewspaper',
            name: '节点余额日报',
            component: () => import('@/views/statistics/dailyNewspaper'),
          },
        ],
      },
    ],
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

const wtiterPath = [
  '/',
  '/registered',
  '/forgetPass',
  '/agreement',
]
router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = window.localStorage.getItem('USER_TOKEN')
  if (!token && !wtiterPath.includes(to.path)) {
    next({ path: '/' })
    return
  }

  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
